/*-----------------------------------------------------
 * File: capture.c
 *
 * Purpose:  Virtual Obj OutDrv 
 *
 *
 * Description:  
 * 
 * Version: 0.01 a0
 *             
 * Copyright 2000 ArSilicii srl
 *-----------------------------------------------------*/
 
#include  <math.h>      
#include "hal.h"
#include "capture.h"

double F1_W,F2_W,Diff_W;
unsigned long C1_W,C2_W;
unsigned char WM,WM_MINUS;


void  Capture_Init(void) {
      WM=NVRAM.Tfmm; WM_MINUS=WM-1;
      C1_W=C2_W=0x1000000l;
}

void  Capture_SetWM(unsigned char w) {
      if (!w) w=1;
      WM=w; WM_MINUS=w-1;
      Capture_Clk();      
}
 
unsigned char Capture_Status(void){
      unsigned char res;
      res=3;
      if (C1_W >=0xFFFFFFl)res&=0x02;
      if (C2_W >=0xFFFFFFl)res&=0x01;      
      return(res);
}

void Capture_Calc_Freq(void){

          if (C1_W > 0 ) F1_W = 240000000.0 / C1_W;
          if (C1_W > (0xFFFFFFl)) F1_W=0.0;
          
          if (C2_W > 0 ) F2_W= 240000000.0 / C2_W;
          if (C2_W > (0XFFFFFFl)) F2_W=0.0;

          Diff_W=fabs(F1_W-F2_W);
}

 
void   Capture_Clk(void) {
          unsigned long capt;          

          capt=HAL_GetPeriodC1();
          if (capt>=0xFFFFFFl) C1_W=0x1000000l; else C1_W= (C1_W*WM_MINUS+capt)/WM; 
          capt=HAL_GetPeriodC2();
          if (capt>=0xFFFFFFl) C2_W=0x1000000l; else C2_W= (C2_W*WM_MINUS+capt)/WM;

}


